package com.yugao.fintech.antelope.pay.payment.controller.payorder;

import com.yugao.fintech.antelope.base.security.annotation.Anonymous;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.pay.common.annotation.PayCheck;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.ClosePayOrderRQ;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.ContinuePayRQ;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.PayOrderQO;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.PayOrderRS;
import com.yugao.fintech.antelope.pay.payment.model.rqrs.payorder.UnifiedOrderExtRQ;
import com.yugao.fintech.antelope.pay.payment.process.payorder.CloseOrderProcess;
import com.yugao.fintech.antelope.pay.payment.process.payorder.QueryOrderProcess;
import com.yugao.fintech.antelope.pay.payment.process.payorder.UnifiedOrderProcess;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.response.Resp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付平台管理端-前端页面, 没有走签名
 */
@Slf4j
@RestController
@RequestMapping("/payMgt")
public class PayOrderMgtController {
    @Autowired
    private QueryOrderProcess queryOrderProcess;
    @Autowired
    private CloseOrderProcess closeOrderProcess;
    @Autowired
    private UnifiedOrderProcess unifiedOrderProcess;

    /**
     * 查单接口
     */
    @OperateLog(isSaveDb = false)
    @PostMapping("/getOrder")
    @PayCheck
    @RamResource(action = "pay:payOrder:get", name = "查询支付订单详情", type = ActionType.READ)
    public Resp<PayOrderRS> getOrder(@RequestBody @Validated PayOrderQO rq) {
        if (StringUtils.isAllEmpty(rq.getMchOrderNo(), rq.getPayOrderNo())) {
            throw new BizException("mchOrderNo 和 payOrderNo不能同时为空");
        }
        return queryOrderProcess.getPayOrder(rq);
    }

    /**
     * 关闭订单
     */
    @PostMapping("/closeOrder")
    @PayCheck
    @RamResource(action = "pay:payOrder:closeOrder", name = "关闭支付订单", type = ActionType.WRITE)
    public Resp<?> closeOrder(@RequestBody @Validated ClosePayOrderRQ rq) {
        return closeOrderProcess.closeOrder(rq);
    }

    /**
     * 统一下单接口 - 用于演示支付功能
     */
    @OperateLog(summary = "统一下单")
    @PostMapping("/unifiedOrder")
    @PayCheck
    @Anonymous
    public Resp<?> unifiedOrder(@RequestBody @Validated UnifiedOrderExtRQ rq) {
        return unifiedOrderProcess.unifiedOrder(rq);
    }

    /**
     * 继续去支付接口, 户很有可能点击付款后, 然后退出去了, 并没有付款, 此时订单处于未支付状态, 用户可以在 <br/>
     * 待支付页面, 继续支付未完成的订单
     */
    @PostMapping("/continuePay")
    @PayCheck
    @RamResource(action = "pay:payOrder:continuePay", name = "继续去支付", type = ActionType.READ)
    public Resp<?> continuePay(@RequestBody @Validated ContinuePayRQ rq) {
        return unifiedOrderProcess.continuePay(rq);
    }
}
